package com.caipu.controller.client;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caipu.common.Result;
import com.caipu.dto.DishDetailDTO;
import com.caipu.dto.DishSearchDTO;
import com.caipu.service.DishSelectionService;
import com.caipu.service.DishService;
import com.caipu.service.UserFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/client/dish")
@CrossOrigin
public class ClientDishController {
    
    @Autowired
    private DishService dishService;
    
    @Autowired
    private DishSelectionService dishSelectionService;
    
    @Autowired
    private UserFavoriteService userFavoriteService;
    
    @GetMapping("/list")
    public Result<Page<DishDetailDTO>> list(DishSearchDTO searchDTO) {
        Page<DishDetailDTO> page = dishService.searchDishes(searchDTO);
        return Result.success(page);
    }
    
    @GetMapping("/{id}")
    public Result<DishDetailDTO> getById(@PathVariable Long id) {
        DishDetailDTO dish = dishService.getDishDetail(id);
        return Result.success(dish);
    }
    
    @GetMapping("/recommended")
    public Result<List<DishDetailDTO>> getRecommendedDishes() {
        List<DishDetailDTO> dishes = dishService.getRecommendedDishes();
        return Result.success(dishes);
    }
    
    @GetMapping("/random-list")
    public Result<List<DishDetailDTO>> getRandomDishes(@RequestParam(required = false) List<Long> categoryIds,
                                                       @RequestParam(required = false) List<Long> tagIds,
                                                       @RequestParam(required = false) List<Long> dishIds) {
        List<DishDetailDTO> dishes = dishService.getRandomDishes(categoryIds, tagIds, dishIds);
        return Result.success(dishes);
    }
    
    @PostMapping("/random")
    public Result<DishDetailDTO> randomSelect(@RequestBody DishSearchDTO searchDTO,
                                             @RequestParam(required = false) Long userId,
                                             @RequestParam(defaultValue = "false") boolean onlyFavorites) {
        DishDetailDTO dish;
        
        if (onlyFavorites && userId != null) {
            // 从收藏列表中随机选择
            List<Long> favoriteDishIds = userFavoriteService.getFavoriteDishIds(userId);
            if (favoriteDishIds.isEmpty()) {
                return Result.error("您还没有收藏任何菜品");
            }
            
            // 随机选择一个收藏的菜品ID
            Random random = new Random();
            Long randomDishId = favoriteDishIds.get(random.nextInt(favoriteDishIds.size()));
            dish = dishService.getDishDetail(randomDishId);
        } else {
            // 从所有菜品中随机选择
            dish = dishService.randomSelectDish(searchDTO);
        }
        
        if (dish == null) {
            return Result.error("没有找到符合条件的菜品");
        }
        return Result.success(dish);
    }
    
    @PostMapping("/selection/accept")
    public Result<Void> acceptDish(@RequestBody AcceptDishRequest request) {
        dishSelectionService.recordSelection(request.getUserId(), request.getDishId(), 1, null);
        return Result.success();
    }
    
    @PostMapping("/selection/reject")
    public Result<Void> rejectDish(@RequestBody RejectDishRequest request) {
        dishSelectionService.recordSelection(request.getUserId(), request.getDishId(), 0, request.getReason());
        return Result.success();
    }
    
    // 内部类定义请求体
    public static class AcceptDishRequest {
        private Long userId;
        private Long dishId;
        
        public Long getUserId() { return userId; }
        public void setUserId(Long userId) { this.userId = userId; }
        public Long getDishId() { return dishId; }
        public void setDishId(Long dishId) { this.dishId = dishId; }
    }
    
    public static class RejectDishRequest {
        private Long userId;
        private Long dishId;
        private String reason;
        
        public Long getUserId() { return userId; }
        public void setUserId(Long userId) { this.userId = userId; }
        public Long getDishId() { return dishId; }
        public void setDishId(Long dishId) { this.dishId = dishId; }
        public String getReason() { return reason; }
        public void setReason(String reason) { this.reason = reason; }
    }
}